<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class UserSubscribe extends Model
{
    //用戶預約表（订单快照）
    protected $table = 'user_subscribe';

    protected $fillable = [
        'user_id',
        'class_base_id',
        'store_id',
        'class_name',
        'store_name',
        'class_classroom_id',
        'class_base_info_id',
        'class_classroom_name',
        'teacher_id',
        'teacher_name',
        'remarks',
        'class_time',
        'end_time',
        'subscribe_status',
        'buy_type',
        'class_info',
        'is_tutoring',
        'is_tutoring',
        'use_integral',
        'refund_data',
        'is_coupon',
        'group_class_id',
        'user_coupon_id',
        'remedial_teaching_time',
        'queue_number',
        'discount_order_id',
        'is_discount',
        'user_vip_use_record_id'
    ];


    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function class_base()
    {
        return $this->belongsTo(ClassBase::class, 'class_base_id', 'id');
    }

    public function store()
    {
        return $this->belongsTo(Store::class, 'store_id', 'id');
    }

    public function classroom()
    {
        return $this->belongsTo(Classroom::class, 'class_classroom_id', 'id');
    }

    public function class_base_info()
    {
        return $this->belongsTo(ClassBaseInfo::class, 'class_base_info_id', 'id');
    }

    public function user_vip_use_record()
    {
        return $this->belongsTo(UserVipUseRecord::class, 'user_vip_use_record_id', 'id');
    }

    public function scopeWhereID($query, $id)
    {
        return $query->where('id', $id);
    }

    public function scopeWhereUserID($query, $user_id)
    {
        return $query->where('user_id', $user_id);
    }

    public function scopeOrderCreatedAt($query, $order)
    {
        return $query->OrderBy('created_at', $order);
    }

    //獲取課室使用数量
    public function ClassUseNumber($class_classroom_id, $start_time, $end_time)
    {
        DB::beginTransaction();
        //dd($class_classroom_id,$start_time,$end_time);
        $number = $this->where('class_classroom_id', $class_classroom_id)
            ->where('class_time', '>=', $start_time)
            ->where('end_time', '<=', $end_time)
            ->whereIn('subscribe_status', [7, 2])
            ->count();
        //dd($number);
        DB::commit();
        return $number;
    }

    /**
     * 是否重複預約
     * @param $class_info_id
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function CheckRepeatClassInfoNumber($class_info_id, $start_time, $end_time, $user_id)
    {
        $number = $this->where('class_base_info_id', $class_info_id)
            ->where('user_id', $user_id)
            ->where('class_time', '>=', $start_time)
            ->where('end_time', '<=', $end_time)
            ->whereIn('subscribe_status', [1, 2])
            ->count();
        return $number;
    }

    /**
     * 課程安排預約人數
     * @param $class_base_info_id 課程安排ID
     * @param $start_time 查詢開始時間
     * @param $end_time 查詢結束時間
     * @param $subscribe_status '预约状态 1排队中 2预约成功 3预约失败 4预约取消 5上课完成 6請假 7已选择补课'
     * @return mixed
     */
    public function ClassSubscribeNumber($class_base_info_id, $start_time, $end_time, $subscribe_status = [1, 2, 5, 7])
    {
        //dd($class_base_info_id);
        //dd($class_base_info_id, $start_time, $end_time);
        $number = $this->whereIn('class_base_info_id', $class_base_info_id)
            ->whereIn('subscribe_status', $subscribe_status)
            ->where('class_time', '>=', $start_time)
            ->where('end_time', '<=', $end_time)
            ->select([
                'class_base_info_id',
                DB::raw('count(*) as total')
            ])
            ->groupBy('class_base_info_id')
            ->get()
            ->toArray();
        //dd($number);
        $number = array_column($number, 'total', 'class_base_info_id');
        //dd($number);
        return $number;
    }


}
